package com.hq.thread.test3;


/*
 * 同步
 * 这里有两种方式加锁
 * 分别是
 * 1.synchronized
 * 2.lock()和unlock()
 */
public class MyRunnable implements Runnable {
    private int tickets = 20;

    @Override
    public void run() {
        while (tickets > 0) {
            synchronized (this) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "售出了第" + (tickets--) + "张票");

                    if (tickets == 0) {
                        System.out.println("售票结束================>");
                    }
                }
            }
        }

    }
}